{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装工具包 (https://tinypng.com)\n",
    "!pip install -i https://mirrors.aliyun.com/pypi/simple/ tinify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入工具包\n",
    "import tinify # \n",
    "import os\n",
    "from img_utils import show_img # 查看图片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取 API Key 的步骤"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 访问 https://tinypng.com/developers\n",
    "\n",
    "2. 如下图：填写 fullname、邮箱地址，点击“Get Your API Key”按钮，即可收到 API Key\n",
    "<img src=\"./tinypng/tinypng_apikey.png\"/>\n",
    "\n",
    "<font color=red>这个网站非常良心，开放了免费的API，而且取消了每张大小的限制，只限定每个月处理 500 张图片。\n",
    "    这对大多数人来说，已经足足有余了</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 定义压缩图片的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取到的API Key，粘贴在这里\n",
    "API_KEY = \"\"\n",
    "\n",
    "def compress_image(source_file, output_file):\n",
    "    \"\"\"\n",
    "    压缩文件\n",
    "    :param source_file: 原始图片路径\n",
    "    :param output_file: 压缩后图片路径\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    tinify.key = API_KEY # 设置 API_KEY\n",
    "    source = tinify.tinify.from_file(source_file)\n",
    "    result = source.to_file(output_file)\n",
    "    if result:\n",
    "        print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 增加知识点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# round方法：保留小数位\n",
    "num = 1234.56789\n",
    "round(num, 2) # 保留两位小数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查看原始图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 原始图片路径\n",
    "src_file = \"./tinypng/blue_bg.png\"\n",
    "# 显示原始图片\n",
    "show_img(src_file)\n",
    "# 获取原始图片的大小(单位：字节)\n",
    "src_file_size = os.path.getsize(src_file)\n",
    "# 返回的图片大小是字节，这里除以1000后，单位则为k字节，并保留两位小数\n",
    "print(\"原始文件路径：%s\" % src_file)\n",
    "print(\"原始文件大小：%sk\" % round(src_file_size/1000, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 压缩图片，并查看压缩后的图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 压缩后的图片路径\n",
    "out_file = \"./tinypng/blue_bg_x.png\"\n",
    "# 压缩文件\n",
    "compress_image(src_file, out_file)\n",
    "print(\"压缩文件路径：%s\" % out_file)\n",
    "# 获取压缩后图片的大小(单位：字节)\n",
    "out_file_size = os.path.getsize(out_file)\n",
    "# 返回的图片大小是字节，这里除以1000后，单位则为k字节，并保留两位小数\n",
    "print(\"压缩文件大小：%sk\" % round(out_file_size/1000, 2))\n",
    "# 显示压缩后的图片\n",
    "show_img(out_file)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
